package compilers.AST.basics;

import compilers.AST.Node;
import compilers.AST.types.TypeSpecifier;
import compilers.symbolTable.SymbolTableElement;
import compilers.symbolTable.SymbolTableScope;
import compilers.symbolTable.SymbolTableVariableElement;

public class VariableParameter extends Parameter {
	
	private TypeSpecifier fType;
	private Identifier fIdentifier;
	private boolean fConstant;

	public VariableParameter() {
		fIdentifier = null;
		fConstant = false;
	}
	
	public VariableParameter(int lineNr) {
		super(lineNr);
		fIdentifier = null;
		fConstant = false;
	}
	
	public void print(int identLevel) {
		println(identLevel, "VariableParameter");
		fType.print(identLevel+1);
		if (fConstant)  {
			println(identLevel, "Constant");
		}
		if (fIdentifier != null) {
			fIdentifier.print(identLevel+1);
		}
	}
	
	public String getSymbolTableOutput() {
		String result = "[";
		result += fType.getTypeName();
		if (fIdentifier != null) {
			result += ", " + fIdentifier.getValue();
		}
		if (fConstant)  {
			result += ", constant";
		}
		result += "]";
		return result;
	}
	
	public SymbolTableElement toSymbolTableElement() {
		return new SymbolTableVariableElement(fIdentifier.getValue(), fType, fConstant, fNodeId, fLineNr);
	}
	
	public boolean compare(Parameter param, SymbolTableScope currentScope) {
		if (!(param instanceof VariableParameter)) {
			return false;
		}
		if (fType.getType(currentScope) != param.getType().getType(currentScope)) {
			return false;
		}
		if (fConstant != param.isConstant()) {
			return false;
		}
		return true;
	}
	
	public void setType(TypeSpecifier type) {
		fType = type;
	}
	
	public boolean isConstant() {
		return fConstant;
	}
	
	public TypeSpecifier getType() {
		return fType;
	}
	
	public void setIdentifier(Identifier ident) {
		fIdentifier = ident;
	}
	
	public void makeConstant() {
		fConstant = true;
	}

}
